using System.IO;
using IWshRuntimeLibrary;

namespace ScottLane.VideoInbox
{
    /// <summary>
    /// A file shortcut.
    /// </summary>
    public class Shortcut
    {
        #region Constructor

        private Shortcut()
        {
        }

        /// <summary>
        /// Initializes a new instance of the Shortcut class with the specified directory, name and target path.
        /// </summary>
        /// <param name="directory">The shortcut directory.</param>
        /// <param name="name">The shortcut name.</param>
        /// <param name="targetPath">The shortcut target path.</param>
        public Shortcut(string directory, string name, string targetPath)
        {
            _directory = directory;
            _name = name;
            _targetPath = targetPath;
        }

        #endregion

        #region Properties

        private string _directory;

        /// <summary>
        /// Gets or sets the shortcut directory.
        /// </summary>
        public string Directory
        {
            get { return _directory; }
            set { _directory = value; }
        }

        private string _name;

        /// <summary>
        /// Gets or sets the shortcut name.
        /// </summary>
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private string _targetPath;

        /// <summary>
        /// Gets or sets the shortcut target path.
        /// </summary>
        public string TargetPath
        {
            get { return _targetPath; }
            set { _targetPath = value; }
        }

        /// <summary>
        /// Gets the shortcut file name extension.
        /// </summary>
        public static string Extension
        {
            get { return ".lnk"; }
        }

        /// <summary>
        /// Gets the shortcut file name.
        /// </summary>
        public string FileName
        {
            get { return Path.Combine(_directory, _name + Extension); }
        }

        #endregion

        #region Methods

        /// <summary>
        /// Returns true if the file is a shortcut.
        /// </summary>
        /// <param name="fileName">The file name.</param>
        /// <returns>True if the file is a shortcut, false otherwise.</returns>
        public static bool IsShortcut(string fileName)
        {
            return new FileInfo(fileName).Extension == Extension;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Shortcut Load(string fileName)
        {
            // todo - parse a shortcut
            return new Shortcut();
        }

        /// <summary>
        /// Gets a value indicating whether the shortcut exists on disk.
        /// </summary>
        public bool Exists
        {
            get { return System.IO.File.Exists(FileName); }
        }

        /// <summary>
        /// Creates the shortcut.
        /// </summary>
        public void Create()
        {
            DirectoryInfo shortcutDirectory = new DirectoryInfo(_directory);

            if (!shortcutDirectory.Exists)
            {
                shortcutDirectory.Create();
            }

            WshShellClass shell = new WshShellClass();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(FileName);
            shortcut.TargetPath = _targetPath;
            shortcut.Save();
        }

        /// <summary>
        /// Deletes the shortcut.
        /// </summary>
        public void Delete()
        {
            if (System.IO.File.Exists(FileName))
            {
                System.IO.File.Delete(FileName);
            }
        }

        #endregion
    }
}
